﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyFisherys : YuanlinUserControl,
        IReplyConverter<CompanyFisheryReply, GetListOperation>,
        IReplyConverter<CompanyFisheryAndAppCategoryRelationReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList companyFisherys;
        private EntityList companyFishery1s;
        public int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);
        public int appCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AppCategoryId);
        public string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        
        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            CompanyFisheryAndAppCategoryRelationListRequest relationRequest = new CompanyFisheryAndAppCategoryRelationListRequest();
            relationRequest.CategoryId = appCategoryId;
            requestBinders.Add(RequestHelper.BuildGetCompanyFisheryAndAppCategoryRelationListRequest(this, relationRequest));

            CompanyFisheryListRequest request = new CompanyFisheryListRequest();
            request.UserId = UserIdFromGreenNum;
            request.CategoryId = categoryId;
            request.TradeType = tradeType;
            request.Verify = 1;
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            requestBinders.Add(RequestHelper.BuildGetCompanyFisheryListRequest(this, request));
        }

        private void BindData()
        {
            EntityList tempList = new EntityList();
            foreach (CompanyFisheryAndAppCategoryRelation relation in companyFishery1s)
            {
                foreach (CompanyFishery item in companyFisherys)
                {
                    if (relation.FisheryId.Value == item.EntityId.Value)
                    {
                        tempList.Add(item);
                    }
                }
            }

            list.DataSource = tempList;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(tempList.TotalCount);
                currentPage.TotalRecords = (tempList.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        /// <summary>
        /// 得到字典列表（首页上的那个七大版块）
        /// </summary>
        /// <param name="companyFisheryId">公司水产Id</param>
        /// <returns>字典Url</returns>
        public string GetDicUrl(int fisheryId)
        {
            if (fisheryId == 0)//如果与网站的水产没对应上的就不用返回链接
            {
                return "javascript:void(0);";
            }

            return SiteUrls.Instance().GetTechUrl(fisheryId,(int)CategoryType.Fishery);
        }
        /// <summary>
        /// 要显示的规格和单位，在维护的时候会在单位的最后加个*(这是和客户约定好的)
        /// </summary>
        /// <param name="categoryId">分类</param>
        /// <param name="flag">0:显示规格数据，1:显示单位数据</param>
        /// <returns></returns>
        public string GetAquacultureGuiGe(int categoryId, int flag)
        {
            AquacultureGuiGeListRequest request = new AquacultureGuiGeListRequest();
            request.CategoryId = categoryId;
            request.Unit = "*";
            EntityList guiges = BusinessManager.GetAquacultureGuiGeList(RequestHelper.BuildGetAquacultureGuiGeListRequest(request));
            return GetResult(guiges, flag);
        }
        private string GetResult(EntityList guiges, int flag)
        {
            string returnvalue = string.Empty;
            string specification = string.Empty;
            string unit = string.Empty;
            if (guiges == null || guiges.Count == 0)
            {
                return returnvalue;
            }
            foreach (AquacultureGuiGe item in guiges)
            {
                specification += "," + item.Title;
                unit += "," + item.Unit.Value.Replace("*", "");
            }
            switch (flag)
            {
                case 0:
                    if (specification.Length > 0)
                    {
                        returnvalue = specification.Substring(1);
                    }
                    break;
                case 1:
                    if (unit.Length > 0)
                    {
                        returnvalue = unit.Substring(1);
                    }
                    break;
            }
            return returnvalue;
        }

        public string GetTarget(int companyFisheryId)
        {
            Aquaculture aquaculture = BusinessManager.GetAquaculture(companyFisheryId);
            if (aquaculture == null)//如果与网站的花木没对应上的就不用返回链接
            {
                return string.Empty;
            }
            return "_blank";
        }

        #region IReplyConverter<CompanyFisheryReply,GetListOperation> Members

        public void ConvertReply(CompanyFisheryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyFisherys = reply.EntityList;

                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion

        #region IReplyConverter<CompanyFisheryAndAppCategoryRelationReply,GetListOperation> 成员

        public void ConvertReply(CompanyFisheryAndAppCategoryRelationReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyFishery1s = reply.EntityList;
            }
        }

        #endregion
    }
}

